import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:zz_listen_english/common/index.dart';

class HttpIpSetController extends GetxController {
  HttpIpSetController();

  GlobalKey fromKey = GlobalKey<FormState>();
  TextEditingController ipController =
      TextEditingController(text: Storage().getString(Constants.ip));
  TextEditingController portController =
      TextEditingController(text: Storage().getString(Constants.port));

//false  http
  //ture https
  bool checkValue = false;

  _initData() {
    var prefixIp = Storage().getString(Constants.prefixIp);
    if (prefixIp == "http") {
      checkValue = false;
    } else {
      checkValue = true;
    }
    update(["http_ip_set"]);
  }

  void onTap() {}

  void onSave() {
    if ((fromKey.currentState as FormState).validate()) {
      Storage().setString(Constants.ip, ipController.text);
      Storage().setString(Constants.port, portController.text);
      if (checkValue == false) {
        Storage().setString(Constants.prefixIp, "http");
      } else {
        Storage().setString(Constants.prefixIp, "https");
      }
      Get.back();
    }
  }

  @override
  void onReady() {
    super.onReady();
    _initData();
  }

  @override
  void onClose() {
    super.onClose();
    ipController.dispose();
    portController.dispose();
  }
}
